<?php

function frontpress_uri_join( $base, $path = '', $args = array() ) {
	return add_query_arg( $args, preg_replace( '#^https?:/#', '$0/', preg_replace( '#//+#', '/',
		$base . '/' . implode( '/', (array) $path ) ) ) );
}

function frontpress_uri( $path = '', $args = array() ) {
	return frontpress_uri_join( backpress_get_option( 'application_uri' ), $path, $args );
}

function frontpress_uri_current() {
	return ( $_SERVER['HTTPS'] ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

/**
 * Makes from an array of arrays a flat array.
 *
 * @param array $array the arra to flatten
 * @return array flattenned array
 */
function frontpress_array_flatten( $array ) {
	$res = array();
    foreach ( $array as $value ) {
    	$res = array_merge( $res, is_array( $value ) ? frontpress_array_flatten( $value ) :
    		( is_a( $value, 'FP_Things' ) ? $value->get() : array( $value ) ) );
    }
    return $res;
}

/**
 * Create a function that returns values from an array in sequence.
 *
 * @param array $array The array to iterate
 * @return callback
 */
function frontpress_sequence( $array ) {
	return create_function( '', 'static $sequence; if ( !isset( $sequence ) ) $sequence = ' . var_export( $array, true ) . '; elseif ( each( $sequence ) === false ) { reset( $sequence ); each( $sequence ); } return current( $sequence );' );
}

function frontpress_length_sort( $a, $b ) {
	return strlen( $a ) - strlen( $b );
}